package org.luckyjourney.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.luckyjourney.entity.dto.UserDTO;
import org.luckyjourney.entity.video.Video;
import org.luckyjourney.entity.video.vo.VideoStartCountVO;
import org.luckyjourney.entity.vo.BasePage;
import org.luckyjourney.holder.UserHolder;
import org.luckyjourney.limit.Limit;
import org.luckyjourney.service.QiNiuFileService;
import org.luckyjourney.service.video.VideoService;
import org.luckyjourney.util.R;
import org.luckyjourney.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xhy
 * @since 2023-10-24
 */
@RestController
@RequestMapping("/luckyjourney/video")
@Api(tags = "视频接口")
public class VideoController {

    @Autowired
    private VideoService videoService;


    @Autowired
    private QiNiuFileService fileService;

    /**
     * 获取文件上传token
     * @return
     */
    @GetMapping("/token")
    @ApiOperation("获取文件上传token")
    public R getToken(){
        return R.ok().data(fileService.getToken());
    }

    /**发布视频/修改视频
     * @param video
     * @return
     */
    @PostMapping
    @Limit(limit = 5,time = 3600L,msg = "发布视频一小时内不可超过5次")
    @ApiOperation("发布视频/修改视频")
    public R publishVideo(@RequestBody @Validated Video video){
        videoService.publishVideo(video);
        return R.ok().message("发布成功,请等待审核");
    }

    /**
     * 删除视频
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除视频")
    public R deleteVideo(@PathVariable Long id){
        videoService.deleteVideo(id);
        return R.ok().message("删除成功");
    }

    /**
     * 查看用户所管理的视频 -稿件管理
     * @param basePage
     * @return
     */
    @GetMapping
    @ApiOperation("查看用户所管理的视频 -稿件管理")
    public R listVideo(BasePage basePage){
        return R.ok().data(videoService.listByUserIdVideo(basePage,UserHolder.get()));
    }


    /**
     * 点赞视频
     */
    @PostMapping("/star/{id}")
    @ApiOperation("点赞视频")
    public Result starVideo(@PathVariable Long id){

        VideoStartCountVO videoStartCountVO = videoService.startVideo(id);

        return Result.success(videoStartCountVO);
    }

    /**
     * 获取点赞视频
     */
    @PostMapping("/pageVideo")
    @ApiOperation("获取点赞视频")
    public R selectVideo(){
        return R.ok().data(videoService.selectVideo());
    }


    /**
     * 添加浏览记录
     * @return
     */
    @PostMapping("/history/{id}")
    @ApiOperation("添加浏览记录")
    public R addHistory(@PathVariable Long id) throws Exception {
        videoService.historyVideo(id, UserHolder.get());
        return R.ok();
    }

    /**
     * 获取用户的浏览记录
     * @return
     */
    @GetMapping("/history")
    @ApiOperation("获取用户的浏览记录")
    public R getHistory(BasePage basePage){
        return R.ok().data(videoService.getHistory(basePage));
    }

    /**
     * 获取收藏夹下的视频
     */
    @GetMapping("/favorites/{favoritesId}")
    @ApiOperation("获取收藏夹下的视频")
    public R listVideoByFavorites(@PathVariable Long favoritesId){
        return R.ok().data(videoService.listVideoByFavorites(favoritesId));
    }

    /**
     * 收藏视频
     * @param fId
     * @param vId
     * @return
     */
    @PostMapping("/favorites/{fId}/{vId}")
    @ApiOperation("收藏视频")
    public Result favoritesVideo(@PathVariable Long fId,@PathVariable Long vId){
        VideoStartCountVO videoStartCountVO =videoService.favoritesVideo(fId,vId);
        return Result.success(videoStartCountVO);
    }

    /**
     * 返回当前审核队列状态
     * @return
     */
    @GetMapping("/audit/queue/state")
    @ApiOperation("返回当前审核队列状态")
    public R getAuditQueueState(){
        return R.ok().message(videoService.getAuditQueueState());
    }


    /**
     * 推送关注的人视频 拉模式
     * @param
     * @return
     */
    @GetMapping("/follow/feed")
    @ApiOperation("推送关注的人视频 拉模式")
    public R followFeed()  {
        final Long userId = UserHolder.get();

        return R.ok().data(videoService.followFeed(userId));
    }

    /**
     * 初始化收件箱
     * @return
     */
    @PostMapping("/init/follow/feed")
    @ApiOperation("初始化收件箱")
    public R initFollowFeed(){
        final Long userId = UserHolder.get();
        videoService.initFollowFeed(userId);
        return R.ok();
    }
    /**
     * 获取咨询师的主页视频
     *
     * @param userDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/getVideo")
    @ApiOperation("获取咨询师的主页视频")
    public R getVideo(@RequestBody UserDTO userDTO) {
        return R.ok().data(videoService.getVideo(userDTO));
    }
}

